package com.iris.live.services.data.repositories.impl;

import com.google.common.collect.Maps;
import com.iris.live.services.data.repositories.QueryExhibitionRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * Created by ThinkPad on 2016/9/19.
 */
@Repository
public class QueryExhibitionRepositoryImpl extends BaseRepository implements QueryExhibitionRepository {

    @Override
    public List getInshopClientRecord(String permit,String entryTime) {

        String sql ="SELECT\n" +
                "    c.`人员姓名` 销售顾问,\n" +
                "    b.`顾客姓名`,\n" +
                "    b.`电话号码`,\n" +
                "    CONCAT(\n" +
                "\n" +
                "        IF (\n" +
                "            d.`品牌` IS NULL\n" +
                "            OR d.`品牌` = '',\n" +
                "            '',\n" +
                "            d.`品牌`\n" +
                "        ),\n" +
                "\n" +
                "    IF (\n" +
                "        d.`级别` IS NULL\n" +
                "        OR d.`级别` = '',\n" +
                "        '',\n" +
                "        CONCAT(\n" +
                "\n" +
                "            IF (\n" +
                "                d.`来源` = 'PbP',\n" +
                "                '-PbP ',\n" +
                "                '-'\n" +
                "            ),\n" +
                "            d.`级别`\n" +
                "        )\n" +
                "    ),\n" +
                "\n" +
                "IF (\n" +
                "    d.`车型` IS NULL\n" +
                "    OR d.`车型` = '',\n" +
                "    '',\n" +
                "    CONCAT('-', d.`车型`)\n" +
                ")\n" +
                "    ) 意向车型,\n" +
                "\n" +
                "IF (\n" +
                "    a.`是否首次到店` = '是',\n" +
                "    a.`顾客来源`,\n" +
                "    a.`顾客再次来源`\n" +
                ") 渠道,\n" +
                " a.`客户级别`,\n" +
                " a.`停留时间` 停留时间（分）,\n" +
                " IF(a.`是否留档`='是', 1, 3) 留档,\n" +
                " IF(a.`是否试驾`='是', 1, 3) 试乘试驾,\n" +
                " IF(a.`是否报价`='是', 1, 3) 报价,\n" +
                " IF(a.`是否订单`='是', 1, 3) 订单,\n" +
                " IF(a.`是否交车`='是', 1, 3) 交车,\n" +
                " a.`是否新留档` 新留档,\n" +
                " a.`顾客编号`,\n" +
                "(SELECT CONCAT(SUM(IF(`是否留档`='是',1,0)),'-', SUM(IF(`是否试驾`='是',1,0)),'-', SUM(IF(`是否报价`='是',1,0)),'-', SUM(IF(`是否订单`='是',1,0)),'-', SUM(IF(`是否交车`='是',1,0)))  \n" +
                "  FROM 顾客进店记录表 B WHERE a.顾客编号 = b.顾客编号 and b.`进店日期` < :entryTime) HISTORY \n" +
                "FROM\n" +
                "    `顾客进店记录表` AS a\n" +
                "left JOIN `顾客记录汇总` AS b ON a.`顾客编号` = b.`顾客编号`\n" +
                "AND b.`许可` = a.`许可`  \n" +
                "left JOIN `用户` AS c ON c.`用户名` =a.`销售顾问`\n" +
                "AND c.`许可` = a.`许可`\n" +
                "left JOIN `LSH车型` AS d ON d.`车型编号` = a.`意向车型A`\n" +
                "WHERE\n" +
                "    a.`许可` =:permit and a.`进店日期` =:entryTime\n";

        Map params = Maps.newHashMap();
        params.put("permit", permit);
        params.put("entryTime",entryTime);
        List<Map<String, Object>> result = this.getList(sql, params);
        String[] history;

            for (Map<String, Object> item : result) { //1：当天是， 2：当天否历史是 3：当天和历史都否
                if (item.get("HISTORY") != null) {
                    history = item.get("HISTORY").toString().split("-");
                } else {
                    continue;
                }

                if (!"1".equalsIgnoreCase(item.get("留档").toString())&&history.length>1) {
                    item.put("留档", history[0].equals("0") ? "3" : "2");
                }

                if (!"1".equalsIgnoreCase(item.get("试乘试驾").toString())&&history.length>2) {
                    System.out.println( history[1]);
                    item.put("试乘试驾", history[1].equals("0") ? "3" : "2");
                }

                if (!"1".equalsIgnoreCase(item.get("报价").toString())&&history.length>3) {
                    item.put("报价", history[2].equals("0") ? "3" : "2");
                }
                if (!"1".equalsIgnoreCase(item.get("订单").toString())&&history.length>4) {
                    item.put("订单", history[3].equals("0") ? "3" : "2");
                }
                if (!"1".equalsIgnoreCase(item.get("交车").toString())&&history.length>5) {
                    item.put("交车", history[4].equals("0") ? "3" : "2");
                }
            }


        return result;
    }
}
